package com.android.citynetworkpin.fragment.member;


import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.FragmentActivity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.android.citynetworkpin.fragment.BaseLazyFragment;
import com.android.citynetworkpin.utils.ConstantsUrl;
import com.android.citynetworkpin.utils.HttpUtils;
import com.android.citynetworkpin.utils.LoginState;
import com.android.citynetworkpin.utils.MyLog;
import com.android.citynetworkpin.utils.ObjectUtils;
import com.android.citynetworkpin.R;
import com.android.citynetworkpin.model.member.RankData;
import com.android.citynetworkpin.model.member.RankDataCallback;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import okhttp3.Call;


/**
 * 拼团王
 */
public class Fragment_Mine_Rank extends BaseLazyFragment {


    @BindView(R.id.rank_note)
    TextView rankNote;
    @BindView(R.id.rank_one_avatar)
    ImageView rankOneAvatar;
    @BindView(R.id.rank_one_name)
    TextView rankOneName;
    @BindView(R.id.rank_one_num)
    TextView rankOneNum;
    @BindView(R.id.rank_two_avatar)
    ImageView rankTwoAvatar;
    @BindView(R.id.rank_two_name)
    TextView rankTwoName;
    @BindView(R.id.rank_two_num)
    TextView rankTwoNum;
    @BindView(R.id.rank_three_avatar)
    ImageView rankThreeAvatar;
    @BindView(R.id.rank_three_name)
    TextView rankThreeName;
    @BindView(R.id.rank_three_num)
    TextView rankThreeNum;
    @BindView(R.id.list_rank)
    LinearLayout listRank;
    @BindView(R.id.click_resetnetwork)
    LinearLayout clickResetnetwork;
    @BindView(R.id.progress)
    RelativeLayout progress;
    @BindView(R.id.no_data)
    RelativeLayout noData;
    @BindView(R.id.no_data_tv)
    TextView noDataTv;
    @BindView(R.id.ll_two)
    LinearLayout llTwo;
    @BindView(R.id.ll_one)
    LinearLayout llOne;
    @BindView(R.id.ll_three)
    LinearLayout llThree;
    @BindView(R.id.ll_list)
    FrameLayout llList;
    @BindView(R.id.item_list_mine_rank_mine_avatar)
    ImageView itemListMineRankMineAvatar;
    @BindView(R.id.item_list_mine_rank_mine_num)
    TextView itemListMineRankMineNum;
    @BindView(R.id.item_list_mine_rank_mine_team)
    TextView itemListMineRankMineTeam;

    private FragmentActivity mcontext;
    private View view;
    private LayoutInflater inflater;
    //    private static final String ARG_PARAM1 = "param1";
//    private String resultid;
    private boolean networkConnected;

    public Fragment_Mine_Rank() {

    }

    public static Fragment_Mine_Rank newInstance() {
        Fragment_Mine_Rank fragment = new Fragment_Mine_Rank();
        Bundle args = new Bundle();
//        args.putString(ARG_PARAM1, param1);
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (getArguments() != null) {
//            resultid = getArguments().getString(ARG_PARAM1);
        }
    }

    @Override
    protected void initPrepare() {

    }

    @Override
    protected void onInvisible() {

    }

    @Override
    protected void initData() {
        GetData();
    }

    @Override
    protected View initView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        this.inflater = inflater;
        view = inflater.inflate(R.layout.fragment_mine_rank, container, false);
        mcontext = getActivity();
        ButterKnife.bind(this, view);
        return view;
    }

    private ArrayList<RankData.DataBean.ListBean> list_data = new ArrayList<>();

    private void GetData() {
        networkConnected = ObjectUtils.isNetworkConnected(mcontext);
        if (networkConnected) {
            String address = ConstantsUrl.domain + ConstantsUrl.MEMBERTUAN_RANK;
            MyLog.e("地址", address);
            HashMap<String, String> header = new HashMap<>();
            String getid = LoginState.getInstance().getid(mcontext);
            String getpd = LoginState.getInstance().getpd(mcontext);
            header.put("UID", getid);
            header.put("UPSW", getpd);
            HttpUtils.get(address, header, null, new RankDataCallback() {

                @Override
                public void onError(Call call, Exception e, int id) {
                    ObjectUtils.GetDataNet(clickResetnetwork, progress, 2);
                }

                @Override
                public void onResponse(RankData response, int id) {
                    if (response == null) {
                        ObjectUtils.GetDataNet(clickResetnetwork, progress, 4);
                        return;
                    }
                    ObjectUtils.GetDataNet(clickResetnetwork, progress, 1);
                    list_data.clear();
                    RankData.DataBean data = response.getData();
                    if (response.getCode() == 0 && data != null) {
                        ObjectUtils.photoCircle(mcontext, data.getPhoto(), itemListMineRankMineAvatar);
                        itemListMineRankMineNum.setText("我的排名：" + data.getSort());
                        itemListMineRankMineTeam.setText("累计：" + data.getSort_num() + "团");
                        rankNote.setText(data.getNote());
                        List<RankData.DataBean.ListBean> list = data.getList();
                        if(list!=null) {
                            switch (list.size()) {
                                case 0:
                                    break;
                                case 1:
                                    llOne.setVisibility(View.VISIBLE);
                                    ObjectUtils.photoCircle(mcontext, list.get(0).getPhoto(), rankOneAvatar);
                                    rankOneName.setText(list.get(0).getNickname());
                                    rankOneNum.setText("累计：" + list.get(0).getNum() + "团");
                                    break;
                                case 2:
                                    llOne.setVisibility(View.VISIBLE);
                                    llTwo.setVisibility(View.VISIBLE);
                                    ObjectUtils.photoCircle(mcontext, list.get(0).getPhoto(), rankOneAvatar);
                                    rankOneName.setText(list.get(0).getNickname());
                                    rankOneNum.setText("累计：" + list.get(0).getNum() + "团");
                                    ObjectUtils.photoCircle(mcontext, list.get(1).getPhoto(), rankTwoAvatar);
                                    rankTwoName.setText(list.get(1).getNickname());
                                    rankTwoNum.setText("累计：" + list.get(1).getNum() + "团");
                                    break;
                                case 3:
                                    llOne.setVisibility(View.VISIBLE);
                                    llTwo.setVisibility(View.VISIBLE);
                                    llThree.setVisibility(View.VISIBLE);
                                    ObjectUtils.photoCircle(mcontext, list.get(0).getPhoto(), rankOneAvatar);
                                    rankOneName.setText(list.get(0).getNickname());
                                    rankOneNum.setText("累计：" + list.get(0).getNum() + "团");
                                    ObjectUtils.photoCircle(mcontext, list.get(1).getPhoto(), rankTwoAvatar);
                                    rankTwoName.setText(list.get(1).getNickname());
                                    rankTwoNum.setText("累计：" + list.get(1).getNum() + "团");
                                    ObjectUtils.photoCircle(mcontext, list.get(2).getPhoto(), rankThreeAvatar);
                                    rankThreeName.setText(list.get(2).getNickname());
                                    rankThreeNum.setText("累计：" + list.get(2).getNum() + "团");
                                    break;
                                default:
                                    llOne.setVisibility(View.VISIBLE);
                                    llTwo.setVisibility(View.VISIBLE);
                                    llThree.setVisibility(View.VISIBLE);
                                    llList.setVisibility(View.VISIBLE);
                                    ObjectUtils.photoCircle(mcontext, list.get(0).getPhoto(), rankOneAvatar);
                                    rankOneName.setText(list.get(0).getNickname());
                                    rankOneNum.setText("累计：" + list.get(0).getNum() + "团");
                                    ObjectUtils.photoCircle(mcontext, list.get(1).getPhoto(), rankTwoAvatar);
                                    rankTwoName.setText(list.get(1).getNickname());
                                    rankTwoNum.setText("累计：" + list.get(1).getNum() + "团");
                                    ObjectUtils.photoCircle(mcontext, list.get(2).getPhoto(), rankThreeAvatar);
                                    rankThreeName.setText(list.get(2).getNickname());
                                    rankThreeNum.setText("累计：" + list.get(2).getNum() + "团");
                                    list_data.addAll(list);
                                    showRankList(list_data, listRank);
                                    break;
                            }
                        }
                    } else {
                        noData.setVisibility(View.VISIBLE);
                        noDataTv.setText(response.getMsg());
                        noDataTv.setVisibility(View.VISIBLE);
                    }
                }
            });
        } else {
            ObjectUtils.GetDataNet(clickResetnetwork, progress, 4);
            ObjectUtils.toast(mcontext, "网络连接出现异常");
        }
    }

    @OnClick(R.id.click_resetnetwork_refresh)
    public void onClick() {
        ObjectUtils.GetDataNet(clickResetnetwork, progress, 3);
        GetData();
    }


    private void showRankList(ArrayList<RankData.DataBean.ListBean> list, LinearLayout addview) {

        addview.removeAllViews();
        for (int i = 3; i < list.size(); i++) {
            View convertView = LayoutInflater.from(mcontext).inflate(R.layout.item_list_mine_rank, null);
            TextView item_list_mine_rank_name = (TextView) convertView.findViewById(R.id.item_list_mine_rank_name);
            TextView item_list_mine_rank_team = (TextView) convertView.findViewById(R.id.item_list_mine_rank_team);
            TextView item_list_mine_rank_num = (TextView) convertView.findViewById(R.id.item_list_mine_rank_num);
            ImageView item_list_mine_rank_avatar = (ImageView) convertView.findViewById(R.id.item_list_mine_rank_avatar);
            RankData.DataBean.ListBean teamListBean = list.get(i);
            ObjectUtils.photoCircle(mcontext, teamListBean.getPhoto(), item_list_mine_rank_avatar);
            item_list_mine_rank_name.setText(teamListBean.getNickname());
            item_list_mine_rank_team.setText("累计：" + teamListBean.getNum() + " 团");
            item_list_mine_rank_num.setText((i + 1) + "");
            addview.addView(convertView);
        }
    }
}
